<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理员 - 贷款审批</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
<style>
    .reject-reason {
        display: none;
    }
</style>
</head>
<body>
    <div class="container">
        <h1 class="mt-4">贷款审批</h1>
        
        <table class="table table-striped mt-4">
            <thead>
                <tr>
                    <th>贷款ID</th>
                    <th>贷款编号</th>
                    <th>申请人</th>
                    <th>金额</th>
                    <th>申请时间</th>
                    <th>到期时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${loans}" var="loan">
                    <tr>
                        <td>${loan.id}</td>
                        <td>${loan.loanNumber}</td>
                        <td>${loan.user.realName}</td>
                        <td>${loan.amount}</td>
                        <td>${loan.createTime}</td>
                        <td>${loan.dueDate}</td>
                        <td>
                            <button type="button" class="btn btn-success approve-btn" data-loan-id="${loan.id}">批准</button>
                            <button type="button" class="btn btn-danger reject-btn" data-loan-id="${loan.id}">拒绝</button>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </div>

    <div class="modal fade" id="rejectModal" tabindex="-1" aria-labelledby="rejectModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="rejectModalLabel">拒绝贷款申请</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="rejectForm">
                        <input type="hidden" id="rejectLoanId" name="loanId">
                        <div class="mb-3">
                            <label for="reason" class="form-label">拒绝原因</label>
                            <textarea class="form-control" id="reason" name="reason" rows="3" required></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-danger" id="confirmReject">确认拒绝</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function() {
            // 批准按钮点击事件
            $('.approve-btn').click(function() {
                var loanId = $(this).data('loan-id');
                if (confirm('确定要批准这笔贷款申请吗？')) {
                    $.post('${pageContext.request.contextPath}/admin/loan', 
                           {action: 'approve', loanId: loanId},
                           function(data) {
                               alert(data);
                               location.reload();
                           });
                }
            });
            
            // 拒绝按钮点击事件
            $('.reject-btn').click(function() {
                var loanId = $(this).data('loan-id');
                $('#rejectLoanId').val(loanId);
                $('#rejectModal').modal('show');
            });
            
            // 确认拒绝按钮点击事件
            $('#confirmReject').click(function() {
                var loanId = $('#rejectLoanId').val();
                var reason = $('#reason').val();
                $.post('${pageContext.request.contextPath}/admin/loan', 
                       {action: 'reject', loanId: loanId, reason: reason},
                       function(data) {
                           alert(data);
                           $('#rejectModal').modal('hide');
                           location.reload();
                       });
            });
        });
    </script>
</body>
</html>